package com.cn.trip.servlet;

import com.cn.trip.bean.User;
import com.cn.trip.service.CartService;
import com.cn.trip.service.CheckOutService;
import com.cn.trip.service.OrderService;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet implementation class CheckOutServlet
 */
@WebServlet("/CheckOut")
public class CheckOutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckOutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String card=request.getParameter("card");
		String password=request.getParameter("password");
		float money=Float.parseFloat(request.getParameter("money"));
		CheckOutService cs=new CheckOutService();
		int flag=cs.checkOut(card, password,money);
		if(flag==0) {
			response.getWriter().print(0);
		}else if(flag==1){
			
			response.getWriter().print(1);
		}else {
			User user=(User)(request.getSession().getAttribute("user"));
			new CartService().clearCart(user.getId());
			new OrderService().add(user.getId(), money);
			response.getWriter().print(2);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
